using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace Lync.Widget.Library.Configuration
{
    public class ActiveDirectoryGroupConfigurationSection : ConfigurationSection, IActiveDirectoryGroupConfiguration
    {
        [ConfigurationProperty("Groups", IsDefaultCollection = true)]
        private ActiveDirectoryGroupElementCollection GroupElementCollection
        {
            get
            {
                var temp = base["Groups"] as ActiveDirectoryGroupElementCollection;
                return temp;
            }
        }

        [ConfigurationProperty("root", IsRequired = true)]
        public string Root
        {
            get
            {
                return this["root"] as string;
            }
            set
            {
                this["root"] = value;
            }
        }

        [ConfigurationProperty("timetowait", IsRequired = false)]
        public int TimeToWait
        {
            get { return Convert.ToInt32(this["timetowait"]); }
            set
            {
                this["timetowait"] = value;
            }
        }

        [ConfigurationProperty("huntgroupquantity", IsRequired = false)]
        public int HuntgroupQuantity
        {
            get { return Convert.ToInt32(this["huntgroupquantity"]); }
            set
            {
                this["huntgroupquantity"] = value;
            }
        }

        public IList<IActiveDirectoryGroup> Groups
        {
            get { return GroupElementCollection.Cast<IActiveDirectoryGroup>().ToList(); }
        }

        public static ActiveDirectoryGroupConfigurationSection Load()
        {
            ActiveDirectoryGroupConfigurationSection section = null;
            try
            {
                section = ConfigurationManager.GetSection("activedirectoryGroups") as ActiveDirectoryGroupConfigurationSection;
            }
            catch (Exception exception)
            {                                
            }
            return section;
        }        
    }
}